package com.application.fabric.users.service;


import com.application.fabric.fabriccontrol.fabricinit.register.RegisterUser;
import com.application.fabric.users.config.encryptAl.Hashencry;
import com.application.fabric.users.config.encryptAl.ReadKey;
import com.application.fabric.users.entity.Evidence;
import com.application.fabric.users.entity.ExtractToken;
import com.application.fabric.users.service.impl.IExsiting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.application.fabric.users.mapper.exitingmapper;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class Exsiting implements IExsiting {
    @Resource
    public ExtractToken token;

//    存证上链码
    @Autowired
    private RegisterUser registerUser;
//    hash设置
    @Autowired
    private Hashencry hashencry;

    @Autowired
    private ReadKey readKey;
//    mapper层调用
    @Autowired
    private exitingmapper exitingmapper;
//    存证
    @Override
    public int intoexsit(Evidence evidence) throws Exception {
        token.setUserid("linshi");
//        对数据库进行操作
        if (token.getUserid() != null){
            evidence.setUserid(evidence.getUserid());
            /*获取当前时间戳*/
            Date date = new Date(System.currentTimeMillis());
            evidence.setTime(date);
            evidence.setExittype("普通存证");
            //   资产id设置
            String hashset= hashencry.hash(evidence.getExistname(),Long.toString(System.currentTimeMillis()));
            evidence.setAssetid(hashset);
//            存证id设置这一步骤要进行上链
            registerUser.init(hashset);
//            然后读取公钥作为存证hash
            evidence.setExithash(readKey.Certificatemsg(hashset));
        }
        else {
            System.out.println("token异常");
        }
//        设置完成以后存数据库

        return exitingmapper.existing(evidence);
    }

//    出证 --不分页
    @Override
    public List<Evidence> outevidenceallservice(String userid) {
        return exitingmapper.outevidenceallmapper(userid);
    }


}
